package cn.itcast.nems.common.captcha.web.o;

import cn.itcast.nems.common.captcha.CaptchaService;
import cn.itcast.nems.common.captcha.bean.CaptchaBean;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/o/captcha")
@RequiredArgsConstructor
public class CaptchaController {
	
    private final CaptchaService captchaService;

    /**
     * 
     * 功能描述：获取图形验证码
     *
     * @return 验证码图片BASE64码 + 验证码对应的TOKEN
     * 
     * @author 刘曙
     *
     * @since 2024年4月14日
     *
     * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    @PostMapping
    public ResponseEntity<CaptchaBean> captcha() {
        return ResponseEntity.status(HttpStatus.CREATED).body(captchaService.generateCaptcha());
    }
    
    /**
     * 确认验证码是否正确
     * @param captcha 用户输入的验证码值
     * @param token 验证码token
     * @return
     */
    @GetMapping("/captcha/{captcha}")
    public ResponseEntity<String> verify(@PathVariable(name="captcha") String captcha, @RequestParam String token) {
    	boolean isValid = captchaService.verify(captcha, token);
    	return isValid ? ResponseEntity.ok("验证通过！") : ResponseEntity.status(HttpStatus.NOT_FOUND).body("验证码已过期或不正确！");
    }
}
